package org.jeecg.modules.iot.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.iot.model.CarLocationResp;
import org.jeecg.modules.iot.model.Location;
import org.jeecg.modules.iot.model.LocationJson;
import org.jeecg.modules.iot.service.ILocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/iot/location")
@Api(tags = "位置服务接口")
public class LocationController {


    @Autowired
    private ILocationService locationService;

    @ApiOperation("添加位置信息")
    @PostMapping("/add")
    public int add(@RequestBody Location location) {
        return locationService.add(location);
    }

    @ApiOperation("根据序列号查询位置")
    @GetMapping("/queryBySeq")
    public Location queryBySeq(@RequestParam("seq") Integer seq, @RequestParam("tableName") String tableName) {
        return locationService.queryBySeq(seq, tableName);
    }

    @ApiOperation("根据设备ID查询位置")
    @GetMapping("/queryByDeviceId")
    public Location queryByDeviceId(@RequestParam("deviceId") String deviceId) {
        return locationService.queryByDeviceId(deviceId);
    }

    @ApiOperation("根据设备ID查询位置")
    @GetMapping("/queryLocationByDeviceId/{imei}")
    public Location queryLocationByDeviceId(@PathVariable String imei) {
        return locationService.queryByDeviceId(imei);
    }

    @ApiOperation("查询指定时间后最近的位置")
    @GetMapping("/queryNearestLocationAfterTime")
    public Location queryNearestLocationAfterTime(@RequestParam("deviceId") String deviceId,
                                                  @RequestParam("timestamp") Timestamp timestamp) {
        return locationService.queryNearestLocationAfterTime(deviceId, timestamp);
    }

    @ApiOperation("查询指定时间前最近的位置")
    @GetMapping("/queryNearestLocationBeforeTime")
    public Location queryNearestLocationBeforeTime(@RequestParam("deviceId") String deviceId,
                                                   @RequestParam("timestamp") Timestamp timestamp) {
        return locationService.queryNearestLocationBeforeTime(deviceId, timestamp);
    }

    @ApiOperation("查询附近设备")
    @GetMapping("/queryNearestDevices")
    public List<CarLocationResp> queryNearestDevices(@RequestParam("currentLatitude") double currentLatitude,
                                                     @RequestParam("currentLongitude") double currentLongitude,
                                                     @RequestParam("range") Integer range) {
        return locationService.queryNearestDevices(currentLatitude, currentLongitude, range);
    }

    @ApiOperation("查询设备静态位置")
    @GetMapping("/queryStaticByDeviceId")
    public Location queryStaticByDeviceId(@RequestParam("deviceId") String deviceId) {
        return locationService.queryStaticByDeviceId(deviceId);
    }

    @ApiOperation("查询设备移动位置")
    @GetMapping("/queryMoveByDeviceId")
    public Location queryMoveByDeviceId(@RequestParam("deviceId") String deviceId) {
        return locationService.queryMoveByDeviceId(deviceId);
    }

    @ApiOperation("查询时间范围内的位置记录")
    @GetMapping("/queryLocationsByTimeRange")
    public List<LocationJson> queryLocationsByTimeRange(@RequestParam("imei") String imei,
                                                        @RequestParam("startTime") Timestamp startTime,
                                                        @RequestParam("endTime") Timestamp endTime) {
        return locationService.queryLocationsByTimeRange(imei, startTime, endTime);
    }

    @ApiOperation("查询前一公里位置")
    @GetMapping("/queryOneKmBefore")
    public Location queryOneKmBefore(@RequestParam("deviceId") String deviceId,
                                     @RequestParam("milOne") Float milOne) {
        return locationService.queryOneKmBefore(deviceId, milOne);
    }
}
